<template>
	<view class="video-container">
		<video :src="videoUrl" controls autoplay class="video-style"></video>
		<view class="title">{{ title }}</view>
		<view class="download-button" @click="downloadVideo">下载视频</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				videoUrl: '', // 视频地址
				title: '' // 视频标题
			};
		},
		onLoad(options) {
			let that = this;
			that.videoUrl = options.videoUrl || '';
			that.title = options.title || '';
			console.log(this.videoUrl);
		},
		methods: {
			// 下载视频的方法
			downloadVideo() {
				// 检查是否有视频地址
				if (!this.videoUrl) {
					uni.showToast({
						title: '视频地址无效',
						icon: 'none'
					});
					return;
				}
				// 调用微信小程序的下载文件接口
				uni.downloadFile({
					url: this.videoUrl,
					success: (res) => {
						if (res.statusCode === 200) {
							// 下载成功，提示用户保存路径
							uni.showToast({
								title: '视频已下载',
								icon: 'success'
							});
							// 可以选择将文件保存到本地相册（需要用户授权）
							uni.saveVideoToPhotosAlbum({
								filePath: res.tempFilePath,
								success: () => {
									uni.showToast({
										title: '已保存到相册',
										icon: 'success'
									});
								},
								fail: () => {
									uni.showToast({
										title: '保存失败，请检查权限',
										icon: 'none'
									});
								}
							});
						} else {
							// 下载失败
							uni.showToast({
								title: '下载失败',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '下载失败',
							icon: 'none'
						});
					}
				});
			}
		}
	};
</script>

<style>
	.video-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
	}

	.video-style {
		width: 100%;
	}

	.title {
		margin-top: 10px;
		font-size: 25px;
		color: #333;
		text-align: center;
	}

	.download-button {
		margin-top: 20px;
		padding: 10px 20px;
		background-color: #007aff;
		color: #fff;
		text-align: center;
		border-radius: 5px;
	}
</style>